var _rowIndex;

//初始化
efform_onload = function ()
{ 
	$("#ef_region_detail").hide();
};

//点击查询按钮
button_query_onclick = function () 
{   	
	efgrid.submitInqu( "ef_grid_r", "ZM", "ZMCP10", "query");
	clearRegion("ef_grid_d");
	$("#ef_region_detail").hide();
}

//点击主表新增按钮
button_insert_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		var now = new Date();
		var year = grid.getCellValueByColumnName(rows[i],"fdPlanYear");
		var fdPlanMonth = grid.getCellValueByColumnName(rows[i],"fdPlanMonth");
		grid.setCellValue(rows[i],2,year+"年"+fdPlanMonth+"月的采煤计划",TYPE_DATA);
	}
	
	efgrid.submitForm( "ef_grid_r", "ZM", "ZMCP10", "insert", true);
} 

//点击主表修改按钮
button_update_onclick = function (){ 
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="草拟")
		{
			EFAlert("只能对草拟的记录进行修改！","警告"); return;
		}
		var now = new Date();
		var year = grid.getCellValueByColumnName(rows[i],"fdPlanYear");
		var fdPlanMonth = grid.getCellValueByColumnName(rows[i],"fdPlanMonth");
		grid.setCellValue(rows[i],2,year+"年"+fdPlanMonth+"月的采煤计划",TYPE_DATA);
	}
	
	efgrid.submitForm( "ef_grid_r", "ZM", "ZMCP10", "update", true);
}

//点击主表删除按钮
button_delete_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="草拟")
		{
			EFAlert("只能对草拟的记录进行删除！","警告"); return;
		}
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM", "ZMCP10", "delete", true);
	});
}

//点击主表上报按钮
button_report_onclick = function (){  
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	if (rows.length > 1) {
		EFAlert("一次只能上报一条记录！","警告"); return;
	}
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="草拟")
		{
			EFAlert("只能上报草拟状态的采煤计划！","警告"); return;
		}
	}
	EFConfirm('你确认要上报吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM", "ZMCP10", "report", true);
	});
}

//主表记录选择
efgrid_onRowClicked = function( grid_id, row_index ){
	if(grid_id == "ef_grid_r"){
		_rowIndex = row_index;
		var grid = efgrid.getGridObject(grid_id);
		var fdProfessionalplanId = grid.getCellValueByColumnName(row_index, "fdKeyId");
		var fdPlanStatus = grid.getCellValueByColumnName(row_index,"fdPlanStatus");
		
		var fdPlanYear = grid.getCellValueByColumnName(row_index,"fdPlanYear");		//从表需要的参数
		var fdPlanMonth = grid.getCellValueByColumnName(row_index,"fdPlanMonth");	//从表需要的参数
		
		//隐藏/显示从表按钮
		if(fdPlanStatus != '草拟')
			hideAndShowSubButtons(false);
		else
			hideAndShowSubButtons(true);
		
		var info = new EiInfo();
		info.set("fdProfessionalplanId",fdProfessionalplanId);
		
		$('#fdProfessionalplanId').val(fdProfessionalplanId);
		$('#fdPlanYear').val(fdPlanYear);
		$('#fdPlanMonth').val(fdPlanMonth);
		
		EiCommunicator.send("ZMCP11", "query", info, ajax_callback_getdetail);  
	}
}

//主表选择成功后的回调
//刷新冲表
var ajax_callback_getdetail= {
	onSuccess: function(eiInfo) { 
		 efwindow.hide();
		 var grid = efgrid.getGridObject("ef_grid_d");
		 grid.refresh(eiInfo);
		 $('#ef_region_detail').show();
	},
	onFail:function(eMsg)
	{
		EFAlert("明细记录查询失败，原因：" + eMsg ,"警告"); 
	}
}

/****************************从表操作******************************/

//从表新增按钮
button_insertdetail_onclick = function (){
	if (isSelected("ef_grid_d") == 0)return; 
	var ei=_getEi();
	var eiBlock=ei.getBlock("result");
	
	var grid = efgrid.getGridObject("ef_grid_d");
	efgrid.submitForm("ef_grid_d", "ZM", "ZMCP11", "insert", true); 
}

//从表修改按钮
button_updatedetail_onclick = function (){
	if (isSelected("ef_grid_d") == 0)return; 
	efgrid.submitForm("ef_grid_d", "ZM", "ZMCP11", "update", true);
}

//从表删除按钮
button_deletedetail_onclick = function (){
	var grid = isSelected("ef_grid_d");
	if (grid == 0) return;  
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r){
			efgrid.submitForm("ef_grid_d", "ZM", "ZMCP11", "delete", true);
		}
	}); 
} 

//新增、修改、删除后的执行方法
efgrid_onAjaxSubmitSuccess = function(gridId, service_name, method_name, eiInfo) {
	if(gridId == "ef_grid_d") {
		if (eiInfo.status != -1) {
			var grid = efgrid.getGridObject("ef_grid_d");
			grid.refresh( eiInfo );
            refresh_main_grid();
		}
		else
		{
			submitCallBackEiMsg = eiInfo.msg;
		}
	} else {
		var grid = efgrid.getGridObject(gridId);
		grid.refresh( eiInfo );
		clearRegion("ef_grid_d");
		$("#ef_region_detail").hide();
	}
};

//点击了新增行之后，给一些默认数据
//主表新增时，给默认数据
efgrid_afterAddNewRow = function(grid_id){ 
	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();
	if(grid_id=="ef_grid_r")
	{
	    var now = new Date();
	    var year = now.getFullYear();       //年
	    var month = now.getMonth() + 1;     //月
	    if(month > 12) {
	    	year = year + 1;				//下一年
	    	month = 1;						//第一个月
	    }
	    if(month < 10) {
	    	month = "0" + month;
	    }
	    var day = now.getDate();            //日 
	    var clock = year + "-" + month + "-";
	    if(day < 10) clock += "0";
	    clock += day + " "; 
	    
		for(var i = 0 ; i < rows.length;i++){
			grid.setCellValue(rows[i],0,"草拟",TYPE_DATA); 		//状态
			grid.setCellValue(rows[i],1,"采煤计划",TYPE_DATA);	//计划类型
			grid.setCellValue(rows[i],2,year+"年"+month+"月的采煤计划",TYPE_DATA);
			grid.setCellValue(rows[i],3,year,TYPE_DATA);
			grid.setCellValue(rows[i],4,month,TYPE_DATA);
			grid.setCellValue(rows[i],5,clock,TYPE_DATA);
			grid.setCellValue(rows[i],6,0.00,TYPE_DATA);
			grid.setCellValue(rows[i],7,0.00,TYPE_DATA);
			grid.setCellValue(rows[i],8,0.00,TYPE_DATA);
			grid.setCellValue(rows[i],9,0.00,TYPE_DATA);
			grid.setCellValue(rows[i],10,0.00,TYPE_DATA);
			grid.setCellValue(rows[i],11,0.00,TYPE_DATA);
			grid.setCellValue(rows[i],12,0.00,TYPE_DATA);
		}
	}
}

//从表单元格编辑后回调的方法
efgrid_onDataCellSaved = function( grid_id, row_index, col_index, cell_value ) 
{
	  var grid = efgrid.getGridObject( grid_id ); 
	  if(grid_id == 'ef_grid_d' && col_index == 1) 
	  { 
		  var info=_getEi();
		  var eiBlock=info.getBlock("cmes.cp.engineering");
		  for(var i=0;i<eiBlock.getRows().length;i++)
		  {
 			  var _value = eiBlock.getCell(i,"fdKeyId");
			  if(_value==cell_value)
			  {
				  var fdDepartmentId = eiBlock.getCell(i,"fdDepartmentId"); 		//施工部门ID				  
				  var fdRecoveryProcess = eiBlock.getCell(i,"fdRecoveryProcess");	//回采工艺ID <充当落煤方法>
				  var fdThickness = eiBlock.getCell(i,"fdThickness");				//平均煤厚
				  var fdDensity = eiBlock.getCell(i,"fdDensity");					//容重
				  var fdLength = eiBlock.getCell(i,"fdLength");						//采长
				  var fdHeight = eiBlock.getCell(i,"fdHeight");						//采高
				  
				  grid.setCellValue(row_index, 2, fdDepartmentId, TYPE_DATA);
				  grid.setCellValue(row_index, 3, fdThickness, TYPE_DATA);
				  grid.setCellValue(row_index, 4, fdDensity, TYPE_DATA);
				  grid.setCellValue(row_index, 5, fdLength, TYPE_DATA);
				  grid.setCellValue(row_index, 6, fdHeight, TYPE_DATA);
				  grid.setCellValue(row_index, 9, fdRecoveryProcess, TYPE_DATA);
				  grid.refreshCell( row_index, 2 , TYPE_DATA );
				  grid.refreshCell( row_index, 3 , TYPE_DATA );
				  grid.refreshCell( row_index, 4 , TYPE_DATA );
				  grid.refreshCell( row_index, 5 , TYPE_DATA );
				  grid.refreshCell( row_index, 6 , TYPE_DATA );
				  grid.refreshCell( row_index, 9 , TYPE_DATA );
			  }
				  
		  }
	  } 
}

/*****************公用方法区******************/
//根据主表选中记录的状态，确定从表的按钮是否显示
var hideAndShowSubButtons=function(status)
{
	if(!status)
	{
		efbutton.setButtonStatus("insertdetail", false);
		efbutton.setButtonStatus("updatedetail", false);
		efbutton.setButtonStatus("deletedetail", false);
	}else
	{
		efbutton.setButtonStatus("insertdetail", true);
		efbutton.setButtonStatus("updatedetail", true);
		efbutton.setButtonStatus("deletedetail", true);
	}
}

//刷新主表方法
refresh_main_grid = function() {
	var ajax_callback =
	{
		onSuccess :
    		function(eiInfo)
			{
				grid = efgrid.getGridObject("ef_grid_r");
				grid.refresh(eiInfo);
				grid.setCurrentRowIndex(_rowIndex);
    		},
  		onFail:
    		function(eMsg)
			{
  				EFAlert("服务调用失败，原因：" + eMsg ,"警告");
			}
	};

	var info = new EiInfo();

  	EiCommunicator.send( "ZMCP10", "query", info, ajax_callback );

}